<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>教练管理</title>
    <link rel="stylesheet" href="../../res/layui/css/layui.css" media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
    <script src="../../res/js/jquery-3.1.1.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../res/layui/layui.js"></script>
    <script src="../../res/js/Tools.js"></script>
    <link rel="stylesheet" type="text/css" href="../AtsEmpCSS/atsEmpCss.css"/>
</head>
<body>
<div class="demoTable">
    <div class="layui-inline">
        <input class="layui-input" name="selectTitle" placeholder="请输入教练员名称" id="selectTitle" autocomplete="off">
    </div>
    <div class="layui-input-inline">
        <input type="text" class="layui-input" id="startTimes" name="startTime" autocomplete="off" placeholder="请输入开始日期" >
    </div>
    <div class="layui-input-inline">
        <input type="text" class="layui-input" id="endTimes" name="endTime" autocomplete="off" placeholder="请输入结束日期" >
    </div>
    <input type="button" id="seachTable" class="layui-btn layui-btn-sm" value="搜索" />
</div>
<table id="demo" lay-filter="test" ></table>
<script>
    var tableTwo = null;
    var table = null;
    layui.use('table', function() {
        var table = layui.table

        tableTwo=table.render({
            elem: '#demo'
            ,height: 500
            ,toolbar: '#toolbarDemo'
            ,url: '../../UserseControllerCoach/selectAll' //数据接口
            ,page: true //开启分页
            ,cols: [[ //表头
                 {type: 'checkbox', fixed: 'left'}
                ,{field: 'coachName', title: '教练员名称', align:'center'}
                ,{field: 'coachExp', title: '教练员特点', align:'center'}
                ,{field: 'expertSpecia', title: '专业', align:'center'}
                ,{field: 'phone', title: '联系方式', align:'center'}
                ,{title: '操作',fixed: 'right', width:200, align:'center', toolbar: '#barDemo'}
            ]]
        });
        //templet: "<div>{{layui.util.toDateString(d.roleTime, 'yyyy-MM-dd HH:mm:ss')}}</div>"
        function loadType(expertSpecia){
            url =  "../../UserseControllerCoach/selectAl";
            data = null;
            $.post(url,data,function (obj) {
                $.each(obj,function (i,item) {
                    $("#expertSpecia").append("<option value='"+item.sportId+"'>"+item.sportName+"</option>");
                });
                $("#expertSpecia").val(expertSpecia);
                form.render();
            },"json");
        };
        $(function(){
            $("#seachTable").on("click",function(){
                //var name = document.getElementById("selectTitle").value;
                //alert(name);
                //执行重载
                //带条件查询
                tableTwo.reload({
                    where: {
                        coachName: $("#selectTitle").val(),
                        startTime:$("#startTimes").val(),
                        endTime:$("#endTimes").val(),
                    },
                    page: { curr: 1 }
                });
                rederTime()
            })
        });
        table.on('tool(test)', function(obj){
            var data = obj.data;//行数据
            //console.log(obj)
            if(obj.event === 'del'){
                layer.confirm('您真的要删除吗?', function(index){
                    obj.del();
                    layer.close(index);
                    //使用ajax进入后台删除数据库中的数据
                    var url = "../../UserseControllerCoach/deleteByPrimaryKey";
                    var data$ = {coachId:data.coachId};
                    //alert(data.expertId);
                    $.post(url,data$,function(obj){
                        if(obj.num==1){
                            layer.msg(obj.msg, {
                                icon: 6,
                                time: 500
                            }, function () {
                                //window.location.reload();
                                parent.layui.table.reload("demo");
                            });
                        }else{
                            layer.msg(obj.msg, {
                                icon: 2,
                                time: 500
                            }, function () {
                                return;
                            });
                        }
                    },"json");

                });

            } else if(obj.event === 'edit') {
                //alert(leaderId);
                var data = obj.data;//行数据

                //iframe层
                layer.open({
                    type: 2,
                    title: '数据修改页面',
                    shadeClose: true,
                    shade: 0.8,
                    area: ['85%', '80%'],
                    content: '../../pages/AtsEmpCoach/OpenAtsEmpCoach.html?id=' + data.coachId, //iframe的url
                    end: function () {
                        //tableTwo.reload();
                        //parent.layui.table.reload("demo");
                    }
                })
            } else if(obj.event === 'more'){
                    var data = obj.data;//行数据
                    //iframe层
                    layer.open({
                        type: 2,
                        title: '更多信息',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['85%', '75%'],
                        content: '../../pages/AtsEmpCoach/MoreAtsEmpCoach.html?id='+data.coachId, //iframe的url
                        /*end : function() {
                            tableTwo.reload();
                        }*/
                    });

            }else if(obj.event === 'export'){
                var url = "../../UserseControllerExpert/exportWord?type=2&id=" + data.coachId;
                location.href = url;
            }
        });
        //头工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){

                case 'addBtn':
                    //iframe层
                    layer.open({
                        type: 2,
                        title: '数据新增页面',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['82%', '80%'],
                        content: '../../pages/AtsEmpCoach/OpenAtsEmpCoach.html', //iframe的url
                        end : function() {
                            //tableTwo.reload();
                            //parent.layui.table.reload("demo");
                        }
                    });
                    break;
                case 'exportAll':
                    var data = checkStatus.data;
                    var ids = "";

                    $.each(data, function(index, item) {
                        ids += item.coachId + ",";
                    });

                    if(ids.length==0){
                        layer.msg("请选择需要操作的行");
                        return;
                    }
                    $.each(data, function(index, item) {
                        setTimeout(function(){
                            var url = "../../UserseControllerExpert/exportWord?type=2&id=" + item.coachId;
                            window.open(url);
                        },1000)
                    });
                    break;
                case 'delMany':
                    var data = checkStatus.data;

                    var ids = "";
                    for (var i = 0; i < data.length; i++) {
                        ids += data[i].coachId + ",";
                    }
                    ids = ids.slice(0, -1);
                    console.info(ids);
                    if(ids==null||ids==""){
                        layer.msg("请至少选择一行！");
                        return;
                    }else{
                        layer.confirm('您真的要删除吗?', function(index) {
                            var url = "../../UserseControllerCoach/deleteMany";
                            var datas = {coachIds: ids};
                            $.post(url, datas, function (objs) {
                                if (objs.num == 1) {
                                    layer.msg(objs.msg, {
                                        icon: 6,
                                        time: 500
                                    }, function () {
                                        window.location.reload();
                                        //window.location.reload();
                                        //parent.layui.table.reload("demo");
                                    });
                                } else {
                                    layer.msg(objs.msg, {
                                        icon: 2,
                                        time: 500
                                    }, function () {
                                        return;
                                    });
                                }
                            }, "json");
                        });
                    }
            };
        });
        //刷新时间
        layui.use('laydate', function () {
            laydate = layui.laydate;
            //开始时间、结束时间
            laydate.render({
                elem: '#startTimes',
                trigger: 'click'
            });

            laydate.render({
                elem: '#endTimes',
                trigger: 'click'
            });
        });

        /**
         * 刷新时间控件
         */
        function rederTime() {
            laydate.render({
                elem: '#startTimes',
                trigger: 'click'
            });

            laydate.render({
                elem: '#endTimes',
                trigger: 'click'
            });
        }
    });
</script>
<script>

</script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container" id="insertTable">
        <a class="layui-btn layui-btn-sm" lay-event="addBtn">新增</a>
        <a class="layui-btn layui-btn-sm" id="delMany" lay-event="delMany"  data-type="reload">批量删除</a>
        <a class="layui-btn layui-btn-sm" lay-event="exportAll">批量导出</a>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="more">查看</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="export">导出</a>
</script>
</body>
</html>